package com.hb.trade.api.routine.controller.util;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hb.core.pojo.enums.SendSMSEnum;
import com.hb.core.pojo.project.evaluation.dto.EvaluationCommitteeDTO;
import com.hb.core.pojo.project.evaluation.dto.EvaluationCommitteeMembersDTO;
import com.hb.trade.client.project.EvaluationCommitteeClient;
import org.apache.shiro.util.CollectionUtils;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.third.sms.service.SmsService;
import org.fzzn.core.util.Enums;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * 发送短信的服务
 *
 * @author tian
 * @date 2022年02月26日 15:48
 */
@Service
public class SendSMSService {

    private static final Logger log = LoggerFactory.getLogger(SendSMSService.class);

    protected final SmsService smsService;

    protected final EvaluationCommitteeClient evaluationCommitteeClient;

    public SendSMSService(SmsService smsService,
                          EvaluationCommitteeClient evaluationCommitteeClient) {
        this.smsService = smsService;
        this.evaluationCommitteeClient = evaluationCommitteeClient;
    }



    /**
     * 发送短信
     * @author tian
     * @date 2022-2-26 15:50
     * @param sendCode 短信模板类型
     * @param params 业务参数 每个短信模板需写明传输的参数信息
     */
    public void send(Integer sendCode, JSONObject params){
        SendSMSEnum sendSMSEnum = Enums.of(SendSMSEnum.class, sendCode);

        switch (sendSMSEnum) {
            case EXPERT_REMINDER:
                // 专家提醒
                sendExpertReminder(params);
                break;
            default:
                log.warn("未找到短信模板---------" + sendCode);
        }

    }

    /**
     * 给专家发送短信提醒
     * @author tian
     * @date 2022-2-26 16:07
     * @param params  evaluationCommitteeId 评委会id
     * 短信模板：专家您好，您被抽取到于${time}在${address}评标，回复1确认，回复0拒绝。
     */
    public void sendExpertReminder(JSONObject params) {
        DateTimeFormatter fa = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        // 评委会id
        Long evaluationCommitteeId = (Long) params.get("evaluationCommitteeId");

        EvaluationCommitteeDTO evaluationCommitteeDTO = new EvaluationCommitteeDTO();
        evaluationCommitteeDTO.setId(evaluationCommitteeId);
        evaluationCommitteeDTO.setDesensitization(0);
        Message<EvaluationCommitteeDTO> message = evaluationCommitteeClient.evaluationCommitteeDetailByPushSms(evaluationCommitteeDTO);
        if (message.isSuccess() && message.getData().getSmsFlag() == 1) {
            evaluationCommitteeDTO = message.getData();

            List<EvaluationCommitteeMembersDTO> expertList = evaluationCommitteeDTO.getExpertList();
            if (!CollectionUtils.isEmpty(expertList)) {
                for (EvaluationCommitteeMembersDTO evaluationCommitteeMembersDTO : expertList) {
                    // 短信参数信息
                    JSONObject pushData = JSONUtil.createObj()
                            .putOpt("date", fa.format(evaluationCommitteeDTO.getEvaluationStartTime()))
                            .putOpt("address", evaluationCommitteeDTO.getEvaluationAddress())
                            .putOpt("password", evaluationCommitteeMembersDTO.getPassword())
                            .putOpt("phone", evaluationCommitteeDTO.getAgencyOrganizeInfo().getContactInformation());
                    smsService.sendSms(evaluationCommitteeMembersDTO.getMemberMobile().trim(), SendSMSEnum.EXPERT_REMINDER.getSendCode(), pushData);
                }
            }
        }
    }

}
